//
//  XLReprotUserVC.m
//  XiangLove
//
//  Created by 李传坤 on 2018/8/7.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLReprotUserVC.h"
#import "BMHPhotoPickManager.h"
#import "UIImage+Clip.h"

@interface XLReprotUserVC ()<UITextViewDelegate>
@property (nonatomic ,strong)UITextView *textView;
@property (nonatomic ,strong)UIButton *imageButon1;
@property (nonatomic ,strong)UIButton *imageButon2;
@property (nonatomic ,strong)UIButton *imageButon3;
@property (nonatomic ,strong)UILabel *qqLabel;
@property (nonatomic ,strong)UILabel *placeHolder;
@property (nonatomic ,strong)UITextField *qqTextField;
@property (nonatomic ,strong)UIButton *tijiaoButton;
@property (nonatomic,   copy) NSString           *imageUrl1;
@property (nonatomic,   copy) NSString           *imageUrl2;
@property (nonatomic,   copy) NSString           *imageUrl3;
@end

@implementation XLReprotUserVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageUrl1 = @"";_imageUrl2 = @"";_imageUrl3 = @"";
    self.navigationItem.title = @"举报/投诉";
    [self showBackButton:NULL];
    //意见输入框
    _textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 5, ScreenWidth-20, 150)];
    _textView.textColor = [UIColor darkGrayColor];_textView.delegate = self;
    _textView.backgroundColor = kClearColor;_textView.tintColor = RGB_HEX(0xffbb01);
    _textView.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:_textView];
    _placeHolder = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 250, 15)];
    _placeHolder.text = @" 请输入您要举报/投诉的内容";
    _placeHolder.textColor = [UIColor lightGrayColor];
    _placeHolder.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:_placeHolder];
    NSInteger width = (ScreenWidth-40)/3;
    _imageButon1 = [UIButton buttonWithType:UIButtonTypeCustom];
    _imageButon1.frame = CGRectMake(10, 160, width, width*4/3);
    _imageButon1.tag = 100;
    [_imageButon1 setBackgroundImage:[UIImage imageNamed:@"common_pic_tianjia1"] forState:UIControlStateNormal];
    [_imageButon1 addTarget:self action:@selector(updateImage2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_imageButon1];
    
    _imageButon2 = [UIButton buttonWithType:UIButtonTypeCustom];_imageButon2.tag = 101;_imageButon2.hidden = YES;
    [_imageButon2 setBackgroundImage:[UIImage imageNamed:@"common_pic_tianjia1"] forState:UIControlStateNormal];
    [_imageButon2 addTarget:self action:@selector(updateImage2:) forControlEvents:UIControlEventTouchUpInside];
    _imageButon2.frame = CGRectMake(width+20, 160, width, width*4/3);

    [self.view addSubview:_imageButon2];
    _imageButon3 = [UIButton buttonWithType:UIButtonTypeCustom];_imageButon3.tag = 102;_imageButon3.hidden = YES;
    [_imageButon3 setBackgroundImage:[UIImage imageNamed:@"common_pic_tianjia1"] forState:UIControlStateNormal];
    [_imageButon3 addTarget:self action:@selector(updateImage2:) forControlEvents:UIControlEventTouchUpInside];
    _imageButon3.frame = CGRectMake(2*width+30, 160, width, width*4/3);

    [self.view addSubview:_imageButon3];
    //QQ标题
    _qqLabel = [[UILabel alloc]init];
    _qqLabel.text = @" 留下您的联系方式，我们会及时给您反馈";
    _qqLabel.textColor = [UIColor darkGrayColor];
    _qqLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:_qqLabel];
    [_qqLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageButon1.mas_bottom).offset(15);
        make.right.equalTo (self.view.mas_right).offset(-10);
        make.left.equalTo(self.view.mas_left).offset(10);
        make.height.mas_equalTo(50);
    }];
    //qq输入框
    _qqTextField = [[UITextField alloc]init];
    _qqTextField.backgroundColor = kClearColor;
    _qqTextField.textColor = [UIColor blackColor];
    _qqTextField.placeholder = @" QQ/手机/微信";
    _qqTextField.keyboardType = UIKeyboardTypeNumberPad;
    _qqTextField.tintColor = RGB_HEX(0xffbb01);
    _qqTextField.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:_qqTextField];
    [_qqTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.qqLabel.mas_bottom).offset(5);
        make.right.equalTo (self.view.mas_right).offset(-10);
        make.left.equalTo (self.view.mas_left).offset(10);
        make.height.mas_equalTo(50);
    }];
    
    
    _tijiaoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_tijiaoButton setTitle:@"提交" forState:UIControlStateNormal];
    _tijiaoButton.backgroundColor = RGB_HEX(0xffbb01);
    _tijiaoButton.layer.cornerRadius = 5;
    [_tijiaoButton addTarget:self action:@selector(tijiaoButton2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_tijiaoButton];
    [_tijiaoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.qqTextField.mas_bottom).offset(25);
        make.left.equalTo(self.view.mas_left).offset(10);
        make.right.equalTo (self.view.mas_right).offset(-10);
        make.height.mas_equalTo(50);
    }];
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    _placeHolder.hidden = YES;
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length == 0) {
        _placeHolder.hidden = NO;
    }
}
- (void)updateImage2:(UIButton *)button{
    [_textView resignFirstResponder];
    [[BMHPhotoPickManager shareInstance] pickFromCustomViewInController:self allowEditing:NO successHandler:^(UIImage *image) {
        CGFloat imagew = CGImageGetWidth(image.CGImage);
        if (imagew > 550){
            CGFloat imageH = CGImageGetHeight(image.CGImage);
            image = [self imageWithImage:image scaledToSize:CGSizeMake(550, imageH*550/imagew)];
        };
        button.userInteractionEnabled = NO;
        [self.navigationItem.rightBarButtonItem setEnabled:NO];
        [XLHTTPSessionManager upLoadImageFileWithImage:image Progress:^(NSNumber *progress) {
            BMHLog(@"%@", progress);
        } Success:^(id success) {
            [XLDownloadImageTool downloadImage:success place:nil button:button];
            [MBProgressHUD showSuccess:@"上传成功" toView:self.view];
            if (button.tag == 100) {
                _imageUrl1 = success;
                _imageButon2.hidden = NO;
            }
            if (button.tag == 101) {
                _imageUrl2 = success;
                _imageButon3.hidden = NO;
            }
            if (button.tag == 102) {
                _imageUrl3 = success;
            }
            button.userInteractionEnabled = YES;
            [self.navigationItem.rightBarButtonItem setEnabled:YES];
        } Failure:^(id error) {
            button.userInteractionEnabled = YES;
            [self.navigationItem.rightBarButtonItem setEnabled:YES];
            [MBProgressHUD showSuccess:@"上传成功" toView:self.view];
        }];
        
    } cancelHandler:^{
        
    }];
}
- (UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize{
    
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
    
}
- (BOOL) isEmpty:(NSString *) str {
    
    if (!str) {
        
        return true;
        
    } else {
        
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            
            return true;
            
        } else {
            
            return false;
            
        }
        
    }
    
}
- (void)tijiaoButton2{
    NSString *picString = _imageUrl1;
    if (_imageUrl2.length > 0) {
        picString = [NSString stringWithFormat:@"%@|%@",_imageUrl1,_imageUrl2];
    }
    if (_imageUrl3.length > 0) {
        picString = [NSString stringWithFormat:@"%@|%@|%@",_imageUrl1,_imageUrl2,_imageUrl3];
    }
   
    if (_textView.text.length == 0 || [self isEmpty:_textView.text]) {
        [MBProgressHUD showContent:@"请输入举报内容"];
        return;
    }
    NSDictionary *dic = @{@"reportUserId":_toUserId,
                          @"reportInfo":_textView.text,
                          @"userId":[[XLAccountManager shareInstance]model].Id ,
                          @"contanct":_qqTextField.text,
                          @"picture":picString};
    @weakify(self)
    [[[XLHTTPSessionManager sharedManager] XLV2POST:@"/Api/v2/AnchorIndex/ReportUser" parameters:dic resultClass:nil] subscribeNext:^(id x) {
        NSLog(@"==%@",x);
        @strongify(self)
        [MBProgressHUD showContent:@"提交成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } error:^(NSError *error) {
        NSLog(@"%@", error);
//        [MBProgressHUD showContent:@"请重试!"];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
